package com.autonomous_printer.impl;

import com.autonomous_printer.mapper.OrderMapper;
import com.autonomous_printer.pojo.Order;
import com.autonomous_printer.service.PayService;
import com.autonomous_printer.service.PrintService;
import com.autonomous_printer.util.PrinterStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class PrintServiceImpl implements PrintService {

    @Autowired
    PayService payService;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    PrinterStatus printerStatus;

    @Value("${print.noneColor}")
    double noneColorValue;

    @Value("${print.color}")
    double colorValue;

    @Override
    public String prePrint(int pageNum, int fileId, String printPageType, String printColorType, int printCount) {
        double money;

        if (printColorType.equals("color")) {
            money = pageNum * colorValue;
        } else {
            money = pageNum * noneColorValue;
        }
        money = money * printCount;

        money = (double) Math.round(money * 100) / 100;
        return payService.placeOrder(money, fileId);
    }


    @Override
    public boolean print(int orderId, int count) {
        Order order = orderMapper.queryOderById(orderId);
        double page = order.getMoney() / 0.2;
        int result = printerStatus.getPage() - (int) page;
        double ink = printerStatus.getInk() - 0.1 * page;
        //打印机纸张或者墨水不足
        if (result < 0 || ink < 0) {
            return false;
        }
        //更新打印机资源打印
        printerStatus.setPage(result);
        printerStatus.setInk(ink);
        //运行打印机
        runPrinter();

        return true;
    }

    /**
     * 运行打印机
     */
    private void runPrinter() {

    }

}
